
package com.scs.application.modules.base.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.model.PageResult;
import com.scs.application.core.model.RestResult;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.mvc.controller.ImAndExAbleController;
import com.scs.application.core.threadLock.ThreadLock;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.base.entity.Hospital;
import com.scs.application.modules.base.entity.HospitalSupplier;
import com.scs.application.modules.base.entity.SupplierApplicant;
import com.scs.application.modules.base.request.HospitalQueryRequest;
import com.scs.application.modules.base.request.HospitalSupplierQueryRequest;
import com.scs.application.modules.base.service.HospitalService;
import com.scs.application.modules.base.service.HospitalSupplierService;
import com.scs.application.modules.base.service.SupplierApplicantService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 医院 controller
 */
@AllArgsConstructor
@RestController
@RequestMapping("/base/hospitals")
public class HospitalController extends CrudController<Hospital, String, HospitalService> implements ImAndExAbleController<HospitalQueryRequest> {
    private HospitalSupplierService hospitalSupplierService;
    private SupplierApplicantService supplierApplicantService;

    @GetMapping("/page")
    public PageResult page(HospitalQueryRequest request) {
        IPage<Hospital> page = service.page(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }


    @GetMapping("/list")
    public CommonResult list(HospitalQueryRequest request) {
        List<Hospital> list = service.list(request);
        return CommonResult.ok("查询成功", list);
    }

    /**
     * 关键字搜索医院信息
     *
     * @param request
     * @param keywords
     * @return
     */
    @GetMapping("/page_by_keywords")
    public CommonResult pageByKeywords(HospitalQueryRequest request, @RequestParam(value = "keywords", required = false) String keywords) {
        IPage page = service.pageByKeywords(request, keywords);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    /**
     * 获取当前登录医院信息
     */
    @GetMapping("/get_current_hospital")
    public CommonResult getCurrentHospital() {
        String hospitalId = UserUtils.getHospitalId();
        Hospital hospital = service.getById(hospitalId);
        return CommonResult.ok("查询成功", hospital);
    }



    /*获取医院信息*/
    @GetMapping("/listUser")
    public CommonResult listUser(HospitalQueryRequest request) {
        QueryWrapper wrapper = Wrappers.<Hospital>query().orderByAsc("name");
        if (!UserUtils.isAdmin()) {
            // 只筛选有供货关系的医院
            if (UserUtils.isSupplier()) {
                HospitalSupplierQueryRequest hospitalSupplierQueryRequest = new HospitalSupplierQueryRequest();
                hospitalSupplierQueryRequest.setSupplierId(UserUtils.getSupplierId());
                List<HospitalSupplier> hospitalSupplierList = hospitalSupplierService.list(hospitalSupplierQueryRequest);
                if (!hospitalSupplierList.isEmpty()) {
                    List<String> hospitalIdList = hospitalSupplierList.stream().map(HospitalSupplier::getHospitalId).distinct().collect(Collectors.toList());
                    wrapper.in("id", hospitalIdList);
                } else{
                    wrapper.eq("1","0");
                }
            }
            // 只筛选当前医院
            if (UserUtils.isHospital()) {
                wrapper.eq("id", UserUtils.getHospitalId());
            }
        }

        List<Hospital> list = service.list(wrapper);
        return CommonResult.ok("查询成功", list);
    }

    /***
     * @param entity
     * @Description:  通用保存、更新
     */
    @PostMapping("/saveOrUpdateCommon")
    @Override
    public RestResult saveOrUpdateCommon(@Valid @RequestBody Hospital entity) {
        if (entity.isNewRecord()) {
            service.saveCascadeById(entity);
        }else{
            service.updateCascadeById(entity);
        }
        return RestResult.ok("数据保存成功", entity);
    }


    /*供货关系申请时选择*/
    @GetMapping("/listForSpHpApply")
    public CommonResult listForSpHpApply() {
        QueryWrapper wrapper = Wrappers.<Hospital>query().orderByAsc("name");
        HospitalSupplierQueryRequest hospitalSupplierQueryRequest = new HospitalSupplierQueryRequest();
        hospitalSupplierQueryRequest.setSupplierId(UserUtils.getSupplierId());
        List<HospitalSupplier> hospitalSupplierList = hospitalSupplierService.list(hospitalSupplierQueryRequest);
        if (!hospitalSupplierList.isEmpty()) {
            List<String> hospitalIdList = hospitalSupplierList.stream().map(HospitalSupplier::getHospitalId).distinct().collect(Collectors.toList());
            wrapper.notIn("id", hospitalIdList);
        }

        List<SupplierApplicant> supplierApplicantList = supplierApplicantService.list(Wrappers.<SupplierApplicant>query()
                .eq("supplier_id",UserUtils.getSupplierId())
                .in("status", Arrays.asList(SupplierApplicant.STATUS_UNSUBMIT,SupplierApplicant.STATUS_SUBMITED))
        );
        if (!supplierApplicantList.isEmpty()) {
            List<String> hospitalIdList = supplierApplicantList.stream().map(SupplierApplicant::getHospitalId).distinct().collect(Collectors.toList());
            wrapper.notIn("id", hospitalIdList);
        }
        List<Hospital> list = service.list(wrapper);
        return CommonResult.ok("查询成功", list);
    }


    /***
     * @Description:  外部系统调用-新增
     */
    @PostMapping("/addBatchFromTpc")
    @ThreadLock(busType = "医院批量新增TPC")
    public RestResult addBatchFromTpc(@Valid @RequestBody List<Hospital> dataList) {
        return RestResult.ok(service.addBatchFromTpc(dataList));
    }


}
